package feeelinganalyzer.crawler;

import java.util.ArrayList;
import java.util.List;

import twitter4j.IDs;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Tweet;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;

public class Spider {

	private int contador;

	public Spider(int valorInicialContador) {
		this.contador = valorInicialContador;
	}
	
	public void saveTweetsFromQuery(String query){
		List<String> doSearch = doSearch(query);
		Writer w = new Writer(contador);
		if(doSearch == null)
			return;
		w.writeToFile(doSearch);
		w.closeFile();
	}

	private List<String> doSearch(String query) {
		QueryResult searchResults = null;
		try {
			searchResults = TwitterFactory.getSingleton().search(
					new Query(query));
		} catch (TwitterException e) {
			System.out
					.println("Algum erro ocorreu na busca. Detalhes do erro: ");
			e.printStackTrace();
			return null;
		}
		List<Tweet> tweets = searchResults.getTweets();
		List<String> messages = new ArrayList<String>();
		for (Tweet tweet : tweets) {
			messages.add(tweet.getText());
		}
		return messages;
	}

	private List<String> getTweetsFromUser(long id) {
		ResponseList<Status> userTimeline = null;
		try {
			userTimeline = TwitterFactory.getSingleton().getUserTimeline(id);
		} catch (TwitterException e) {
			System.out
					.println("Aconteceu algum erro na hora de buscar os tweets do usuario "
							+ id);
			e.printStackTrace();
			return null;
		}
		List<String> tweets = new ArrayList<String>();
		for (Status status : userTimeline) {
			tweets.add(status.getText());
		}
		return tweets;
	}

	private User getUser(long id) {
		User twitterUser = null;
		try {
			twitterUser = TwitterFactory.getSingleton().showUser(id);
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return twitterUser;
	}

	private User getUser(String userName) {
		User twitterUser = null;
		try {
			twitterUser = TwitterFactory.getSingleton().showUser(userName);
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return twitterUser;
	}

	public void startCrawlingFromUser(String userName) {
		User user = getUser(userName);
		if (user != null)
			startCrawlingFromUser(user.getId());
	}

	private void startCrawlingFromUser(long id) {
		System.out.println("Comecar a salvar para o usuario de id = " + id);
		IDs friendsIDs = null;
		try {
			friendsIDs = TwitterFactory.getSingleton().getFriendsIDs(id, -1);
		} catch (TwitterException e) {
			System.out.println("Ocorreu algum erro ao obter os ids de " + id);
			e.printStackTrace();
			return;
		}
		Writer writer = new Writer(contador);
		writer.writeToFile(getTweetsFromUser(id));
		long[] iDs = friendsIDs.getIDs();
		for (long l : iDs) {
			System.out.println("Pegando dados do amigo de id = " + l);
			User user = getUser(l);
			if (user.isProtected()) {
				continue;
			}
			List<String> tweetsFromUser = getTweetsFromUser(l);
			if (tweetsFromUser != null) {
				writer.writeToFile(tweetsFromUser);
			} else {
				break;
			}
		}
		contador++;
		writer.closeFile();
	}
}
